package com.xgo.http;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xgo.bean.Shop;
import com.xgo.bean.ShopContent;

/**
 * <p>名称:com.xgo.http.XgoCrawler.java</p>
 * <p>描述:爬虫</p>
 * <p>日期:2013-11-12 下午08:16:23</p>
 * 
 * @author  wangxw
 * @version 1.0
 *
 */
public class XgoCrawler {
	/** 爬取的页数*/
	private int page;
	private static Logger logger = LoggerFactory.getLogger(XgoCrawler.class);
	
	public static void main(String[] args) {
		XgoCrawler crawler = new XgoCrawler();
		crawler.page = 67;
		crawler.run("http://dealer.xgo.com.cn/list_p2_n1.html");
	}
	
	/**
	 * 递归执行商品列表信息解析
	 * @param szUrl
	 */
	public void run(String szUrl){
//		logger.info("URL:" + szUrl);
	//	System.out.println("URL:" + szUrl);
		if(!szUrl.contains("http://")){
			szUrl = "http://dealer.xgo.com.cn" + szUrl;
		}
		if(page > 0){
			//下载页面
			Document doc = null;
			try {
				doc = Jsoup.parse(new URL(szUrl), 60000);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(doc == null ){
				logger.error(" Xgo html down load failed.");
				return;
			}
			//获取页面信息 商品列表、是否包含下一页、当前页码
			ShopContent sc = parseShop(doc);
			List<Shop> list = sc.getList();
			if(list == null || list.size() == 0){
				logger.error(" Xgo product parse failed.");
				return;
			}else{
				for (int i = 0; i < list.size(); i++) {
			//		logger.info("店名："+(list.get(i).getShopName() +" | 地址：" + list.get(i).getAddress()));
					System.out.println(list.get(i).getShopName()+":" + list.get(i).getAddress()+":" +list.get(i).getVendor()+":" +list.get(i).getPhone());
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			e.printStackTrace();
			}
			page--;
			//递归调用 解析下一页
			if(sc.getNextLinkUrl() != null){
				run(sc.getNextLinkUrl());
			}
		
			}
		}
		

	/**
	 * 解析
	 * @param szHtml
	 * @return
	 */
	private ShopContent parseShop(Document doc) {
		ShopContent sc = new ShopContent();
		List<Shop> list = new ArrayList<Shop>();
		
		//获取下一页
		String nextLinkUrl= doc.getElementsByAttributeValue("class", "next").attr("href");
		//获取当前页
		int nowPage = 0;
		String now = doc.getElementsByAttributeValueStarting("class", "active").text();
	//	System.out.println("当前页码："+now);
		if(now == null || now.trim().equals("")){
		}else{
			nowPage = Integer.parseInt(now);
		}
		sc.setNextLinkUrl(nextLinkUrl);
		sc.setNowPage(nowPage);
		//获取页面商品
		Elements es = doc.getElementsByAttributeValue("class", "dealer_list");
		es = es.get(0).getElementsByTag("li");
		for (Element element : es) {
			Shop shop = parseContent(element);
			list.add(shop);
		}
		sc.setList(list);
		return sc;
	}
	
	/**
	 * 解析4S店及地址
	 * @param element
	 * @return
	 */
	private Shop parseContent(Element e) {
		Shop shop = new Shop();
		shop.setShopName(e.getElementsByTag("p").get(0).text().trim());
		String address = e.getElementsByTag("p").get(2).text();
		address = address.substring(address.indexOf("：")+1);
		address = address.substring(0, address.indexOf("["));
		shop.setAddress(address.trim());
		shop.setVendor(e.getElementsByTag("p").get(1).text());
		Elements elements = e.getElementsByAttributeValueStarting("class", "mg");
		if(elements != null && elements.size() > 0){
			shop.setPhone(elements.get(0).text());
		}else{
			shop.setPhone(e.getElementsByTag("p").get(3).text());
		}
		return shop;
	}
}
